// D:\java-project\JavaFx\src\main\java\com\heima\ConnectionManager.java
package com.heima;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

import java.util.ArrayList;
import java.util.List;

public class ConnectionManager {
    private List<ConnectionLine> connections = new ArrayList<>();

    public void addConnection(ConnectionPoint from, ConnectionPoint to) {
        ConnectionLine line = new ConnectionLine(from, to);
        connections.add(line);
    }

    public void drawAll(GraphicsContext gc) {
        for (ConnectionLine line : connections) {
            line.draw(gc);
        }
    }

    public void updateAll(DrawableShape movedShape) {
        connections.stream().filter(line -> line.getStart().getOwner() == movedShape || line.getEnd().getOwner() == movedShape)
                .forEach(line -> line.updatePositions(line.getStart().getOwner(), line.getEnd().getOwner()));
    }
//for (ConnectionLine line : connections) {
//            if (line.getStart().getOwner() == movedShape || line.getEnd().getOwner() == movedShape) {
//                line.updatePositions(line.getStart().getOwner(), line.getEnd().getOwner());
//            }
//        }
    public List<ConnectionLine> getAllConnections() {
        return new ArrayList<>(connections);
    }

    public void removeConnection(ConnectionLine line) {
        connections.remove(line);
    }

    public List<ConnectionLine> getConnections() {
        return connections;
    }
}